package org.news.web.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.news.web.constant.AuthorInfo;
import org.news.web.constant.CategoriesConstant;
import org.news.web.constant.CategoriesStatus;
import org.news.web.domain.Categories;
import org.news.web.domain.vo.CreatorsVo;
import org.news.web.service.impl.CategoriesServiceImpl;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.CategoriesVo;
import org.news.web.domain.bo.CategoriesBo;
import org.news.web.service.ICategoriesService;

/**
 * 栏目或类别
 *
 * @author Gu Ci
 * @date 2024-03-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/categories")
public class CategoriesController extends BaseController {

    private final ICategoriesService categoriesService;


    private final  CategoriesServiceImpl cs;

    /**
     * 查询栏目或类别列表
     */
    @SaCheckPermission("web:categories:list")
    @GetMapping("/list")
    public R<List<CategoriesVo>> list(CategoriesBo bo) {
        List<CategoriesVo> list = categoriesService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 查询栏目或类别状态
     */
    @GetMapping("/status")
    public R<List<CategoriesStatus>> CategoriesStatus(){
        return R.ok(CategoriesConstant.CategoriesStatuses());
    }

    /**
     * 导出栏目或类别列表
     */
    @SaCheckPermission("web:categories:export")
    @Log(title = "栏目或类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CategoriesBo bo, HttpServletResponse response) {
        List<CategoriesVo> list = categoriesService.queryList(bo);
        ExcelUtil.exportExcel(list, "栏目或类别", CategoriesVo.class, response);
    }


    /**
     * 获取栏目或类别详细信息
     *
     * @param categoryId 主键
     */
    @SaCheckPermission("web:categories:query")
    @GetMapping("/{categoryId}")
    public R<CategoriesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long categoryId) {
        return R.ok(categoriesService.queryById(categoryId));
    }

    /**
     * 新增栏目或类别
     */
    @SaCheckPermission("web:categories:add")
    @Log(title = "栏目或类别", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CategoriesBo bo) {
        return toAjax(categoriesService.insertByBo(bo));
    }

    /**
     * 修改栏目或类别
     */
    @SaCheckPermission("web:categories:edit")
    @Log(title = "栏目或类别", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CategoriesBo bo) {

        return categoriesService.updateByBo(bo);
    }

    /**
     * 删除栏目或类别
     *
     * @param categoryIds 主键串
     */
    @SaCheckPermission("web:categories:remove")
    @Log(title = "栏目或类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] categoryIds) {
        return categoriesService.deleteWithValidByIds(List.of(categoryIds),true);
    }

    /**
     * test 测试
     */
    @SaIgnore
    @RequestMapping("/getChildContentCounts/{id}")
    public R<Void> test(@PathVariable("id") Long id, HttpServletRequest request){
        System.out.println(request.getHeader("Authorization"));
        return R.warn("和："+cs.getChildContentCounts(id));
    }


    /**
     * 模糊查询
     */

    @GetMapping("/getListCategories")
    public TableDataInfo<CategoriesVo> getListCategories(AuthorInfo bo, PageQuery pageQuery) {
        return categoriesService.getListCategories(bo, pageQuery);
    }


    /**
     *  选择类别
     */
    @GetMapping("/getListLbCategories")
    public TableDataInfo<CategoriesVo> getListLbCategories(AuthorInfo bo, PageQuery pageQuery) {
        return categoriesService.getListLbCategories(bo, pageQuery);
    }
}
